/*
* @(#)DmsRoot.java
*
* Copyright (c) 2003 DCIVision Ltd
* All rights reserved.
*
* This software is the confidential and proprietary information of DCIVision
* Ltd ("Confidential Information").  You shall not disclose such Confidential
* Information and shall use it only in accordance with the terms of the license
* agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.dms.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import com.dcivision.dms.DmsErrorConstant;
import com.dcivision.dms.bean.DmsDocument;
import com.dcivision.dms.bean.DmsRoot;
import com.dcivision.dms.web.ListDmsRootForm;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.ErrorConstant;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.SystemParameterConstant;
import com.dcivision.framework.SystemParameterFactory;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.UserInfoFactory;
import com.dcivision.framework.Utility;
import com.dcivision.framework.bean.AbstractBaseObject;
import com.dcivision.framework.dao.AbstractDAObject;
import com.dcivision.framework.web.AbstractSearchForm;

/**
  DmsRootDAObject.java

  This class is the data access bean for table "DMS_ROOT".

  @author      Zoe Shum
  @company     DCIVision Limited
  @creation date   31/07/2003
  @version     $Revision: 1.38.2.1 $
  */

public class DmsRootDAObject extends AbstractDAObject {

  public static final String REVISION = "$Revision: 1.38.2.1 $";

  public static final String TABLE_NAME = "DMS_ROOT";

  public DmsRootDAObject(SessionContainer sessionContainer, Connection dbConn) {
    super(sessionContainer, dbConn);
  }

  
  protected void initDBSetting() {
    this.baseTableName = TABLE_NAME;
    this.vecDBColumn.add("ID");
    this.vecDBColumn.add("ROOT_NAME");
    this.vecDBColumn.add("ROOT_TYPE");
    this.vecDBColumn.add("LOC_ID");
    this.vecDBColumn.add("STORAGE_LIMIT");
    this.vecDBColumn.add("OWNER_ID");
    this.vecDBColumn.add("RECORD_STATUS");
    this.vecDBColumn.add("UPDATE_COUNT");
    this.vecDBColumn.add("CREATOR_ID");
    this.vecDBColumn.add("CREATE_DATE");
    this.vecDBColumn.add("UPDATER_ID");
    this.vecDBColumn.add("UPDATE_DATE");
  }

  
  protected synchronized AbstractBaseObject getByID(Integer id) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.ROOT_NAME, A.ROOT_TYPE, A.LOC_ID, A.STORAGE_LIMIT, A.OWNER_ID, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE, B.DESCRIPTION, B.ID AS ROOT_FOLDER_ID ");
        sqlStat.append("FROM   DMS_ROOT A, DMS_DOCUMENT B ");
        sqlStat.append("WHERE  A.ID = ? AND (A.RECORD_STATUS = ? OR A.RECORD_STATUS = ?) AND A.ID = B.ROOT_ID AND B.PARENT_ID = 0 ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, id);
        this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, 3, GlobalConstant.RECORD_STATUS_PENDING);
        rs = preStat.executeQuery();
        if (rs.next()) {
          DmsRoot tmpDmsRoot = new DmsRoot();
          tmpDmsRoot.setID(getResultSetInteger(rs, "ID"));
          tmpDmsRoot.setRootName(getResultSetString(rs, "ROOT_NAME"));
          tmpDmsRoot.setRootType(getResultSetString(rs, "ROOT_TYPE"));
          tmpDmsRoot.setRootDesc(getResultSetString(rs, "DESCRIPTION"));
          tmpDmsRoot.setRootFolderID(getResultSetInteger(rs, "ROOT_FOLDER_ID"));
          tmpDmsRoot.setLocID(getResultSetInteger(rs, "LOC_ID"));
          tmpDmsRoot.setStorageLimit(getResultSetDouble(rs, "STORAGE_LIMIT"));
          tmpDmsRoot.setOwnerID(getResultSetInteger(rs, "OWNER_ID"));
          tmpDmsRoot.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpDmsRoot.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpDmsRoot.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpDmsRoot.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpDmsRoot.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpDmsRoot.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpDmsRoot.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsRoot.getCreatorID()));
          tmpDmsRoot.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsRoot.getUpdaterID()));
          return(tmpDmsRoot);
        } else {
          //throw new ApplicationException(ErrorConstant.DB_RECORD_NOT_FOUND_ERROR);
          return null;
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  
  protected synchronized List getList(AbstractSearchForm searchForm) throws ApplicationException {
    PreparedStatement preStat = null;
    PreparedStatement preStatCnt = null;
    ResultSet rs = null;
    ResultSet rsCnt = null;
    StringBuffer sqlStat = new StringBuffer();
    StringBuffer sqlStatCnt = new StringBuffer();
    List result = new ArrayList();
    int totalNumOfRecord = 0;
    int rowLoopCnt = 0;
    int startOffset = TextUtility.parseInteger(searchForm.getCurStartRowNo());
    int pageSize = TextUtility.parseInteger(searchForm.getPageOffset());

    ListDmsRootForm dmsRootForm = (ListDmsRootForm)searchForm;

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.ROOT_NAME, A.ROOT_TYPE, A.LOC_ID, A.STORAGE_LIMIT, A.OWNER_ID, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE, B.DESCRIPTION, B.ID AS ROOT_FOLDER_ID ");
        sqlStat.append("FROM   DMS_ROOT A, DMS_DOCUMENT B ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? AND A.ID = B.ROOT_ID AND B.PARENT_ID = 0 ");
        //sqlStat.append(this.getIDFilterWhereClause("A"));
        if (!Utility.isEmpty(dmsRootForm.getRootType())) {
          sqlStat.append("AND  A.ROOT_TYPE = ? ");
        }

        // Form the WHERE clause for filtering.
        /*
        if (searchForm.isSearchable()) {
          String searchField = searchForm.getBasicSearchField();
          if (searchField.indexOf(".") < 0) {
            searchField = "A." + searchField;
          }
          sqlStat.append("AND  " + searchField + " " + searchForm.getBasicSearchType() + " ? ");
        }*/
        if (searchForm.isSearchable()) {
          String searchField = getSearchColumn(searchForm.getBasicSearchField());
          sqlStat.append("AND  " + searchField + " " + searchForm.getBasicSearchType() + " ? ");
        }

        //format the sql for any 'LIKE' statement contained
        sqlStat = this.getFormattedSQL(sqlStat.toString());

        // Form the ORDER clause for sorting.
        if (searchForm.isSortable()) {
          String sortAttribute = searchForm.getSortAttribute();
          if (sortAttribute.indexOf(".") < 0) {
            sortAttribute = "A." + sortAttribute;
          }
          sqlStat.append("ORDER BY " + sortAttribute + " " + searchForm.getSortOrder());
        }

        // Get total number of record return.
        sqlStatCnt = this.getSelectCountSQL(sqlStat);
        preStatCnt = dbConn.prepareStatement(sqlStatCnt.toString());
        this.setPrepareStatement(preStatCnt, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        int tmpCnt = 2;
        if (!Utility.isEmpty(dmsRootForm.getRootType())) {
          this.setPrepareStatement(preStatCnt, tmpCnt++, dmsRootForm.getRootType());
        }
        if (searchForm.isSearchable()) {
          String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
          this.setPrepareStatement(preStatCnt, tmpCnt, searchKeyword);
        }
        rsCnt = preStatCnt.executeQuery();
        if (rsCnt.next()) {
          totalNumOfRecord = rsCnt.getInt(1);
        }
        try { rsCnt.close(); } catch (Exception ignore) {} finally { rsCnt = null; }
        try { preStatCnt.close(); } catch (Exception ignore) {} finally { preStatCnt = null; }

        // Retrieve the result in row basis.
        sqlStat = this.getSelectListSQL(sqlStat, startOffset, pageSize);
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        tmpCnt = 2;
        if (!Utility.isEmpty(dmsRootForm.getRootType())) {
          this.setPrepareStatement(preStat, tmpCnt++, dmsRootForm.getRootType());
        }
        if (searchForm.isSearchable()) {
          String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
          this.setPrepareStatement(preStat, tmpCnt, searchKeyword);
        }
        rs = preStat.executeQuery();

        this.positionCursor(rs, startOffset, pageSize);

        while (rs.next() && rowLoopCnt<pageSize) {
          DmsRoot tmpDmsRoot = new DmsRoot();
          tmpDmsRoot.setID(getResultSetInteger(rs, "ID"));
          tmpDmsRoot.setRootName(getResultSetString(rs, "ROOT_NAME"));
          tmpDmsRoot.setRootType(getResultSetString(rs, "ROOT_TYPE"));
          tmpDmsRoot.setRootDesc(getResultSetString(rs, "DESCRIPTION"));
          tmpDmsRoot.setRootFolderID(getResultSetInteger(rs, "ROOT_FOLDER_ID"));
          tmpDmsRoot.setLocID(getResultSetInteger(rs, "LOC_ID"));
          tmpDmsRoot.setStorageLimit(getResultSetDouble(rs, "STORAGE_LIMIT"));
          tmpDmsRoot.setOwnerID(getResultSetInteger(rs, "OWNER_ID"));
          tmpDmsRoot.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpDmsRoot.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpDmsRoot.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpDmsRoot.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpDmsRoot.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpDmsRoot.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpDmsRoot.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsRoot.getCreatorID()));
          tmpDmsRoot.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsRoot.getUpdaterID()));
          tmpDmsRoot.setRecordCount(totalNumOfRecord);
          tmpDmsRoot.setRowNum(startOffset++);
          ++rowLoopCnt;
          result.add(tmpDmsRoot);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
        try { rsCnt.close(); } catch (Exception ignore) {} finally { rsCnt = null; }
        try { preStatCnt.close(); } catch (Exception ignore) {} finally { preStatCnt = null; }
      }
    }
  }

  
  protected synchronized List getList() throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.ROOT_NAME, A.ROOT_TYPE, A.LOC_ID, A.STORAGE_LIMIT, A.OWNER_ID, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE, B.DESCRIPTION, B.ID AS ROOT_FOLDER_ID ");
        sqlStat.append("FROM   DMS_ROOT A, DMS_DOCUMENT B ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? AND A.ID = B.ROOT_ID AND B.PARENT_ID = 0 ");
        //sqlStat.append(this.getIDFilterWhereClause("A"));
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        while (rs.next()) {
          DmsRoot tmpDmsRoot = new DmsRoot();
          tmpDmsRoot.setID(getResultSetInteger(rs, "ID"));
          tmpDmsRoot.setRootName(getResultSetString(rs, "ROOT_NAME"));
          tmpDmsRoot.setRootType(getResultSetString(rs, "ROOT_TYPE"));
          tmpDmsRoot.setRootDesc(getResultSetString(rs, "DESCRIPTION"));
          tmpDmsRoot.setRootFolderID(getResultSetInteger(rs, "ROOT_FOLDER_ID"));
          tmpDmsRoot.setLocID(getResultSetInteger(rs, "LOC_ID"));
          tmpDmsRoot.setStorageLimit(getResultSetDouble(rs, "STORAGE_LIMIT"));
          tmpDmsRoot.setOwnerID(getResultSetInteger(rs, "OWNER_ID"));
          tmpDmsRoot.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpDmsRoot.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpDmsRoot.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpDmsRoot.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpDmsRoot.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpDmsRoot.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpDmsRoot.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsRoot.getCreatorID()));
          tmpDmsRoot.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsRoot.getUpdaterID()));
          result.add(tmpDmsRoot);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  
  protected void validateInsert(AbstractBaseObject obj) throws ApplicationException {
    DmsRoot curRecord = (DmsRoot) obj;
    // Check duplicate public folder name exists
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();

    if (DmsRoot.PUBLIC_ROOT.equals(curRecord.getRootType())) {
      synchronized(dbConn) {
        try {
          sqlStat.append("SELECT ID ");
          sqlStat.append("FROM   DMS_ROOT A ");
          sqlStat.append("WHERE  A.ROOT_NAME = ? AND A.RECORD_STATUS = ? ");
          //sqlStat.append(this.getIDFilterWhereClause("A"));
          preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
          this.setPrepareStatement(preStat, 1, curRecord.getRootName());
          this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
          rs = preStat.executeQuery();
          if (rs.next()) {
            throw new ApplicationException(DmsErrorConstant.DUPLICATE_PUBLIC_FOLDER, null, curRecord.getRootName());
          }
        } catch (ApplicationException appEx) {
          throw appEx;
        } catch (SQLException sqle) {
          log.error(sqle, sqle);
          throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
        } catch (Exception e) {
          log.error(e, e);
          throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
        } finally {
          try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
          try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
        }
      }
    }
  }

  
  protected void validateUpdate(AbstractBaseObject obj) throws ApplicationException {
    DmsRoot curRecord = (DmsRoot) obj;
    // Check duplicate public folder name exists
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();

    if (DmsRoot.PUBLIC_ROOT.equals(curRecord.getRootType())) {
      synchronized(dbConn) {
        try {
          sqlStat.append("SELECT ID ");
          sqlStat.append("FROM   DMS_ROOT A ");
          sqlStat.append("WHERE  A.ROOT_NAME = ? AND A.RECORD_STATUS = ? AND A.ID <> ? ");
          //sqlStat.append(this.getIDFilterWhereClause("A"));
          preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
          this.setPrepareStatement(preStat, 1, curRecord.getRootName());
          this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
          this.setPrepareStatement(preStat, 3, curRecord.getID());
          rs = preStat.executeQuery();
          if (rs.next()) {
            throw new ApplicationException(DmsErrorConstant.DUPLICATE_PUBLIC_FOLDER, null, curRecord.getRootName());
          }
        } catch (ApplicationException appEx) {
          throw appEx;
        } catch (SQLException sqle) {
          log.error(sqle, sqle);
          throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
        } catch (Exception e) {
          log.error(e, e);
          throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
        } finally {
          try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
          try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
        }
      }
    }
  }

  
  protected void validateDelete(AbstractBaseObject obj) throws ApplicationException {
  }

  
  protected synchronized AbstractBaseObject insert(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    DmsRoot tmpDmsRoot = (DmsRoot)((DmsRoot)obj).clone();

    synchronized(dbConn) {
      try {
        Integer nextID = getNextPrimaryID();
        Timestamp currTime = Utility.getCurrentTimestamp();
        sqlStat.append("INSERT ");
        sqlStat.append("INTO   DMS_ROOT(ID, ROOT_NAME, ROOT_TYPE, LOC_ID, STORAGE_LIMIT, OWNER_ID, RECORD_STATUS, UPDATE_COUNT, CREATOR_ID, CREATE_DATE, UPDATER_ID, UPDATE_DATE) ");
        sqlStat.append("VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, nextID);
        setPrepareStatement(preStat, 2, tmpDmsRoot.getRootName());
        setPrepareStatement(preStat, 3, tmpDmsRoot.getRootType());
        setPrepareStatement(preStat, 4, tmpDmsRoot.getLocID());
        setPrepareStatement(preStat, 5, tmpDmsRoot.getStorageLimit());
        setPrepareStatement(preStat, 6, tmpDmsRoot.getOwnerID());
        setPrepareStatement(preStat, 7, GlobalConstant.RECORD_STATUS_ACTIVE);
        setPrepareStatement(preStat, 8, new Integer(0));
        setPrepareStatement(preStat, 9, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 10, currTime);
        setPrepareStatement(preStat, 11, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 12, currTime);
        preStat.executeUpdate();
        tmpDmsRoot.setID(nextID);
        tmpDmsRoot.setCreatorID(sessionContainer.getUserRecordID());
        tmpDmsRoot.setCreateDate(currTime);
        tmpDmsRoot.setUpdaterID(sessionContainer.getUserRecordID());
        tmpDmsRoot.setUpdateDate(currTime);
        tmpDmsRoot.setUpdateCount(new Integer(0));
        tmpDmsRoot.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsRoot.getCreatorID()));
        tmpDmsRoot.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsRoot.getUpdaterID()));
        return(tmpDmsRoot);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_INSERT_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  
  protected synchronized AbstractBaseObject update(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    DmsRoot tmpDmsRoot = (DmsRoot)((DmsRoot)obj).clone();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        Timestamp currTime = Utility.getCurrentTimestamp();
        sqlStat.append("UPDATE DMS_ROOT ");
        sqlStat.append("SET  ROOT_NAME=?, ROOT_TYPE=?, LOC_ID=?, STORAGE_LIMIT=?, OWNER_ID=?,RECORD_STATUS=?, UPDATE_COUNT=?, UPDATER_ID=?, UPDATE_DATE=? ");
        sqlStat.append("WHERE  ID=? AND UPDATE_COUNT=? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, tmpDmsRoot.getRootName());
        setPrepareStatement(preStat, 2, tmpDmsRoot.getRootType());
        setPrepareStatement(preStat, 3, tmpDmsRoot.getLocID());
        setPrepareStatement(preStat, 4, tmpDmsRoot.getStorageLimit());
        setPrepareStatement(preStat, 5, tmpDmsRoot.getOwnerID());
        setPrepareStatement(preStat, 6, tmpDmsRoot.getRecordStatus());
        setPrepareStatement(preStat, 7, new Integer(tmpDmsRoot.getUpdateCount().intValue() + 1));
        setPrepareStatement(preStat, 8, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 9, currTime);
        setPrepareStatement(preStat, 10, tmpDmsRoot.getID());
        setPrepareStatement(preStat, 11, tmpDmsRoot.getUpdateCount());
        updateCnt = preStat.executeUpdate();
        if (updateCnt == 0) {
          throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        } else {
          tmpDmsRoot.setUpdaterID(sessionContainer.getUserRecordID());
          tmpDmsRoot.setUpdateDate(currTime);
          tmpDmsRoot.setUpdateCount(new Integer(tmpDmsRoot.getUpdateCount().intValue() + 1));
          tmpDmsRoot.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsRoot.getCreatorID()));
          tmpDmsRoot.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsRoot.getUpdaterID()));
          return(tmpDmsRoot);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_UPDATE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  
  protected synchronized AbstractBaseObject delete(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    DmsRoot tmpDmsRoot = (DmsRoot)((DmsRoot)obj).clone();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        sqlStat.append("DELETE ");
        sqlStat.append("FROM   DMS_ROOT ");
        sqlStat.append("WHERE  ID=? AND UPDATE_COUNT=? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, tmpDmsRoot.getID());
        setPrepareStatement(preStat, 2, tmpDmsRoot.getUpdateCount());
        updateCnt = preStat.executeUpdate();
        if (updateCnt == 0) {
          throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        } else {
          return(tmpDmsRoot);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_DELETE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  
  protected synchronized void auditTrail(String opMode, AbstractBaseObject obj) throws ApplicationException {
    Vector oldValues = new Vector();
    Vector newValues = new Vector();
    DmsRoot tmpDmsRoot = (DmsRoot)this.oldValue;

    if (tmpDmsRoot != null) {
      oldValues.add(toAuditTrailValue(tmpDmsRoot.getID()));
      oldValues.add(toAuditTrailValue(tmpDmsRoot.getRootName()));
      oldValues.add(toAuditTrailValue(tmpDmsRoot.getRootType()));
      oldValues.add(toAuditTrailValue(tmpDmsRoot.getLocID()));
      oldValues.add(toAuditTrailValue(tmpDmsRoot.getStorageLimit()));
      oldValues.add(toAuditTrailValue(tmpDmsRoot.getOwnerID()));
      oldValues.add(toAuditTrailValue(tmpDmsRoot.getRecordStatus()));
      oldValues.add(toAuditTrailValue(tmpDmsRoot.getUpdateCount()));
      oldValues.add(toAuditTrailValue(tmpDmsRoot.getCreatorID()));
      oldValues.add(toAuditTrailValue(tmpDmsRoot.getCreateDate()));
      oldValues.add(toAuditTrailValue(tmpDmsRoot.getUpdaterID()));
      oldValues.add(toAuditTrailValue(tmpDmsRoot.getUpdateDate()));
    }

    tmpDmsRoot = (DmsRoot)obj;
    if (tmpDmsRoot != null) {
      newValues.add(toAuditTrailValue(tmpDmsRoot.getID()));
      newValues.add(toAuditTrailValue(tmpDmsRoot.getRootName()));
      newValues.add(toAuditTrailValue(tmpDmsRoot.getRootType()));
      newValues.add(toAuditTrailValue(tmpDmsRoot.getLocID()));
      newValues.add(toAuditTrailValue(tmpDmsRoot.getStorageLimit()));
      newValues.add(toAuditTrailValue(tmpDmsRoot.getOwnerID()));
      newValues.add(toAuditTrailValue(tmpDmsRoot.getRecordStatus()));
      newValues.add(toAuditTrailValue(tmpDmsRoot.getUpdateCount()));
      newValues.add(toAuditTrailValue(tmpDmsRoot.getCreatorID()));
      newValues.add(toAuditTrailValue(tmpDmsRoot.getCreateDate()));
      newValues.add(toAuditTrailValue(tmpDmsRoot.getUpdaterID()));
      newValues.add(toAuditTrailValue(tmpDmsRoot.getUpdateDate()));
    }

    auditTrailBase(opMode, oldValues, newValues);
  }

  /***********************************************************************
   * DON'T Modify the codes above unless you know what you are doing!!!  *
   * Put your own functions beblow.                                      *
   * For FINDER methods, the function name should be in the notation:    *
   *   public Object getObjectBy<Criteria>()                             *
   *   - e.g. public Object getObjectByCode()                            *
   *   public List getListBy<Criteria>()                                 *
   *   - e.g. public List getListByUserID()                              *
   * For OPERATION methods, the function name should be in the notation: *
   *   public void <Operation>ObjectBy<Criteria>()                       *
   *   - e.g. public void deleteObjectByCode()                           *
   *   public void <Operation>ListBy<Criteria>()                         *
   *   - e.g. public void deleteListByUserID()                           *
   ***********************************************************************/
  
/**
 * Gets the personal root folder by root owner ID and record Status
 * 
 * @param ownerID the personal root owner
 * @param recordStatus the record status
 * @return the DmsRoot Object
 * @throws ApplicationException
 */
  public synchronized DmsRoot getPersonalRootByOwnerIDAndType(Integer ownerID, String recordStatus) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    DmsRoot tmpDmsRoot = new DmsRoot();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.ROOT_NAME, A.ROOT_TYPE, A.LOC_ID, A.STORAGE_LIMIT, A.OWNER_ID, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   DMS_ROOT A ");
        sqlStat.append("WHERE A.OWNER_ID = ? AND A.ROOT_TYPE = ? AND A.RECORD_STATUS = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, ownerID);
        setPrepareStatement(preStat, 2, DmsRoot.PERSONAL_ROOT);
        setPrepareStatement(preStat, 3, recordStatus);
        rs = preStat.executeQuery();

        if (rs.next()) {

          tmpDmsRoot.setID(getResultSetInteger(rs, "ID"));
          tmpDmsRoot.setRootName(getResultSetString(rs, "ROOT_NAME"));
          tmpDmsRoot.setRootType(getResultSetString(rs, "ROOT_TYPE"));
          tmpDmsRoot.setLocID(getResultSetInteger(rs, "LOC_ID"));
          tmpDmsRoot.setStorageLimit(getResultSetDouble(rs, "STORAGE_LIMIT"));
          tmpDmsRoot.setOwnerID(getResultSetInteger(rs, "OWNER_ID"));
          //tmpDmsRoot.setRootDesc(getResultSetString(rs, "DESCRIPTION"));
          //tmpDmsRoot.setRootFolderID(getResultSetInteger(rs, "ROOT_FOLDER_ID"));
          tmpDmsRoot.setRecordStatus(getResultSetString(rs,"RECORD_STATUS"));
          tmpDmsRoot.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpDmsRoot.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpDmsRoot.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpDmsRoot.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpDmsRoot.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpDmsRoot.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsRoot.getCreatorID()));
          tmpDmsRoot.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsRoot.getUpdaterID()));
        }
        return tmpDmsRoot;
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle);
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  
  /**
   * Gets the list of dmsRoot objects by owner ID, root type and record status
   * @param ownerID the owner ID
   * @param rootType the root Type, the type include 
   * {@link com.dcivision.dms.bean.DmsRoot#PUBLIC_ROOT},
   * {@link com.dcivision.dms.bean.DmsRoot#PERSONAL_ROOT}
   * 
   * @param recordStatus the record status
   * @return list of dmsRoot objects  
   * @throws ApplicationException
   */
  public synchronized List getRootByOwnerIDAndType(Integer ownerID, String rootType, String recordStatus) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List list = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.ROOT_NAME, A.ROOT_TYPE, A.LOC_ID, A.STORAGE_LIMIT, A.OWNER_ID, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE, B.DESCRIPTION, B.ID AS ROOT_FOLDER_ID ");
        sqlStat.append("FROM   DMS_ROOT A, DMS_DOCUMENT B ");

        sqlStat.append("WHERE A.OWNER_ID = ? AND A.ROOT_TYPE = ? AND A.RECORD_STATUS = ? AND A.ID = B.ROOT_ID AND B.PARENT_ID = 0 ");
        sqlStat.append("ORDER BY A.ROOT_NAME ASC ");
        //log.debug("sql = " + sqlStat.toString());
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, ownerID);
        setPrepareStatement(preStat, 2, rootType);
        setPrepareStatement(preStat, 3, recordStatus);
        rs = preStat.executeQuery();
        while (rs.next()) {
          DmsRoot tmpDmsRoot = new DmsRoot();
          tmpDmsRoot.setID(getResultSetInteger(rs, "ID"));
          tmpDmsRoot.setRootName(getResultSetString(rs, "ROOT_NAME"));
          tmpDmsRoot.setRootType(getResultSetString(rs, "ROOT_TYPE"));
          tmpDmsRoot.setRootDesc(getResultSetString(rs, "DESCRIPTION"));
          tmpDmsRoot.setRootFolderID(getResultSetInteger(rs, "ROOT_FOLDER_ID"));
          tmpDmsRoot.setStorageLimit(getResultSetDouble(rs, "STORAGE_LIMIT"));
          tmpDmsRoot.setLocID(getResultSetInteger(rs, "LOC_ID"));
          tmpDmsRoot.setOwnerID(getResultSetInteger(rs, "OWNER_ID"));
          tmpDmsRoot.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpDmsRoot.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpDmsRoot.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpDmsRoot.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpDmsRoot.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsRoot.getCreatorID()));
          tmpDmsRoot.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsRoot.getUpdaterID()));
          list.add(tmpDmsRoot);
        }
        return list;
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle);
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  /**
   * Gets the list of the public root by the AbstractSearchForm object
   *
   * @param searchForm the AbstractSearchForm object
   * @return list the public dmsRoot object
   * @throws ApplicationException
   */
  public synchronized List getPublicRootList(AbstractSearchForm searchForm) throws ApplicationException {
    PreparedStatement preStat = null;
    PreparedStatement preStatCnt = null;
    ResultSet rs = null;
    ResultSet rsCnt = null;
    StringBuffer sqlStat = new StringBuffer();
    StringBuffer sqlStatCnt = new StringBuffer();
    List result = new ArrayList();
    int totalNumOfRecord = 0;
    int rowLoopCnt = 0;
    int startOffset = TextUtility.parseInteger(searchForm.getCurStartRowNo());
    int pageSize = TextUtility.parseInteger(searchForm.getPageOffset());
    boolean showNoAccessFolder=SystemParameterFactory.getSystemParameterBoolean(SystemParameterConstant.DMS_SHOW_NO_ACCESS_FOLDER);

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, B.ID AS DOCUMENT_ID, A.ROOT_NAME, A.ROOT_TYPE, A.LOC_ID, A.STORAGE_LIMIT, A.OWNER_ID, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE, B.DESCRIPTION, B.ID AS ROOT_FOLDER_ID ");
        sqlStat.append("FROM   DMS_ROOT A, DMS_DOCUMENT B ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? AND A.ID = B.ROOT_ID AND B.PARENT_ID = 0 AND B.DOCUMENT_TYPE = ? AND A.ROOT_TYPE = ? ");

        // Form the WHERE clause for filtering.
        /*
        if (searchForm.isSearchable()) {
          String searchField = searchForm.getBasicSearchField();
          if (searchField.indexOf(".") < 0) {
            searchField = "A." + searchField;
          }
          sqlStat.append("AND  " + searchField + " " + searchForm.getBasicSearchType() + " ? ");
        }*/

        if (searchForm.isSearchable()) {
          String searchField = getSearchColumn(searchForm.getBasicSearchField());
          sqlStat.append("AND  " + searchField + " " + searchForm.getBasicSearchType() + " ? ");
        }

        // Check Permission.
        if(!showNoAccessFolder){
          List listID = this.sessionContainer.getPermissionManager().getAccessibleObjectIDList(dbConn,GlobalConstant.OBJECT_TYPE_DOCUMENT, new Integer(0));
          if (Utility.isEmpty(listID)) {
            sqlStat.append(" AND B.ID < 0 ");  // Hide all folders.
          } else {
            sqlStat.append(" AND (B.ID=" + ((Integer)listID.get(0)).toString());
            for (int i = 1 ; i < listID.size(); i++) {
              sqlStat.append(" OR B.ID=" + ((Integer)listID.get(i)).toString());
            }
            sqlStat.append(") ");
          }
        }

        // Form the ORDER clause for sorting.
        if (searchForm.isSortable()) {
          String sortAttribute = searchForm.getSortAttribute();
          if (sortAttribute.indexOf(".") < 0) {
            sortAttribute = "A." + sortAttribute;
          }
          sqlStat.append("ORDER BY " + sortAttribute + " " + searchForm.getSortOrder());
        }

        // Get total number of record return.
        sqlStatCnt = this.getSelectCountSQL(sqlStat);
        preStatCnt = dbConn.prepareStatement(sqlStatCnt.toString());
        this.setPrepareStatement(preStatCnt, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStatCnt, 2, DmsDocument.FOLDER_TYPE);
        this.setPrepareStatement(preStatCnt, 3, DmsRoot.PUBLIC_ROOT);
        if (searchForm.isSearchable()) {
          String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
          this.setPrepareStatement(preStatCnt, 4, searchKeyword);
        }
        rsCnt = preStatCnt.executeQuery();
        if (rsCnt.next()) {
          totalNumOfRecord = rsCnt.getInt(1);
        }
        try { rsCnt.close(); } catch (Exception ignore) {} finally { rsCnt = null; }
        try { preStatCnt.close(); } catch (Exception ignore) {} finally { preStatCnt = null; }

        // Retrieve the result in row basis.
        sqlStat = this.getSelectListSQL(sqlStat, startOffset, pageSize);
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, 2, DmsDocument.FOLDER_TYPE);
        this.setPrepareStatement(preStat, 3, DmsRoot.PUBLIC_ROOT);
        if (searchForm.isSearchable()) {
          String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
          this.setPrepareStatement(preStat, 4, searchKeyword);
        }
        rs = preStat.executeQuery();

        this.positionCursor(rs, startOffset, pageSize);

        while (rs.next() && rowLoopCnt<pageSize) {
          DmsRoot tmpDmsRoot = new DmsRoot();
          tmpDmsRoot.setID(getResultSetInteger(rs, "ID"));
          tmpDmsRoot.setDocumentID(getResultSetInteger(rs, "DOCUMENT_ID"));
          tmpDmsRoot.setRootName(getResultSetString(rs, "ROOT_NAME"));
          tmpDmsRoot.setRootType(getResultSetString(rs, "ROOT_TYPE"));
          tmpDmsRoot.setRootDesc(getResultSetString(rs, "DESCRIPTION"));
          tmpDmsRoot.setRootFolderID(getResultSetInteger(rs, "ROOT_FOLDER_ID"));
          tmpDmsRoot.setLocID(getResultSetInteger(rs, "LOC_ID"));
          tmpDmsRoot.setStorageLimit(getResultSetDouble(rs, "STORAGE_LIMIT"));
          tmpDmsRoot.setOwnerID(getResultSetInteger(rs, "OWNER_ID"));
          tmpDmsRoot.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpDmsRoot.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpDmsRoot.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpDmsRoot.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpDmsRoot.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpDmsRoot.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpDmsRoot.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsRoot.getCreatorID()));
          tmpDmsRoot.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsRoot.getUpdaterID()));
          tmpDmsRoot.setRecordCount(totalNumOfRecord);
          tmpDmsRoot.setRowNum(startOffset++);
          ++rowLoopCnt;
          result.add(tmpDmsRoot);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
        try { rsCnt.close(); } catch (Exception ignore) {} finally { rsCnt = null; }
        try { preStatCnt.close(); } catch (Exception ignore) {} finally { preStatCnt = null; }
      }
    }
  }

  /**
   * Gets the public root list which current user has the permission to access.
   *
   * @return list of the public dmsRoot object.
   * @throws ApplicationException
   */
  public synchronized List getPublicRootList() throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();
    int totalNumOfRecord = 0;

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.ROOT_NAME, A.ROOT_TYPE, A.LOC_ID, A.STORAGE_LIMIT, A.OWNER_ID, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE, B.DESCRIPTION, B.ID AS ROOT_FOLDER_ID ");
        sqlStat.append("FROM   DMS_ROOT A, DMS_DOCUMENT B ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? AND A.ID = B.ROOT_ID AND B.PARENT_ID = 0 AND B.DOCUMENT_TYPE = ? AND A.ROOT_TYPE = ? ");

        // Check Permission.
        List listID = this.sessionContainer.getPermissionManager().getAccessibleObjectIDList(GlobalConstant.OBJECT_TYPE_DOCUMENT, new Integer(0));
        if (Utility.isEmpty(listID)) {
          sqlStat.append(" AND B.ID < 0 ");  // Hide all folders.
        } else {
          sqlStat.append(" AND (B.ID=" + ((Integer)listID.get(0)).toString());
          for (int i = 1 ; i < listID.size(); i++) {
            sqlStat.append(" OR B.ID=" + ((Integer)listID.get(i)).toString());
          }
          sqlStat.append(") ");
        }

        sqlStat.append("ORDER BY A.ROOT_NAME ASC ");

        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, 2, DmsDocument.FOLDER_TYPE);
        this.setPrepareStatement(preStat, 3, DmsRoot.PUBLIC_ROOT);
        rs = preStat.executeQuery();

        while (rs.next()) {
          DmsRoot tmpDmsRoot = new DmsRoot();
          tmpDmsRoot.setID(getResultSetInteger(rs, "ID"));
          tmpDmsRoot.setRootName(getResultSetString(rs, "ROOT_NAME"));
          tmpDmsRoot.setRootType(getResultSetString(rs, "ROOT_TYPE"));
          tmpDmsRoot.setRootDesc(getResultSetString(rs, "DESCRIPTION"));
          tmpDmsRoot.setRootFolderID(getResultSetInteger(rs, "ROOT_FOLDER_ID"));
          tmpDmsRoot.setLocID(getResultSetInteger(rs, "LOC_ID"));
          tmpDmsRoot.setStorageLimit(getResultSetDouble(rs, "STORAGE_LIMIT"));
          tmpDmsRoot.setOwnerID(getResultSetInteger(rs, "OWNER_ID"));
          tmpDmsRoot.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpDmsRoot.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpDmsRoot.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpDmsRoot.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpDmsRoot.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpDmsRoot.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpDmsRoot.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsRoot.getCreatorID()));
          tmpDmsRoot.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsRoot.getUpdaterID()));
          tmpDmsRoot.setRecordCount(totalNumOfRecord);
          result.add(tmpDmsRoot);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  /**
   * Gets the used space of the specified root folder
   *
   * @param rootID the root dmsRoot object Id
   * @return the total document size under the root
   * @throws ApplicationException
   */
  public synchronized Double getUsedSpaceByRootID(Integer rootID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List list = new ArrayList();
    Double result = null;

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT SUM( V.ITEM_SIZE )   ");
        sqlStat.append("FROM   DMS_DOCUMENT D, DMS_VERSION V ");
        sqlStat.append("WHERE   D.ID = V.DOCUMENT_ID ");
        sqlStat.append("       AND D.DOCUMENT_TYPE = ? AND D.ROOT_ID = ? ");

        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        //this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, 1, DmsDocument.DOCUMENT_TYPE);
        this.setPrepareStatement(preStat, 2, rootID);

        rs = preStat.executeQuery();
        if (rs.next()) {
          result = this.getResultSetDouble(rs, 1);
        }
        if (result == null) {
          result = new Double(0.0);
        }
        return result;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle);
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  
  /**
   * Gets list of the public DmsRoot objects
   * 
   * @return list of the public DmsRoot objects
   * @throws ApplicationException
   */
  public synchronized List getAllPublicRootList() throws ApplicationException {
        return getAllPublicRootListForWorkflow(null);
  }
  public synchronized List getAllPublicRootListForWorkflow(String orderBy) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.ROOT_NAME, A.ROOT_TYPE, A.LOC_ID, A.STORAGE_LIMIT, A.OWNER_ID, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE, B.DESCRIPTION, B.ID AS ROOT_FOLDER_ID ");
        sqlStat.append("FROM   DMS_ROOT A, DMS_DOCUMENT B ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? AND A.ID = B.ROOT_ID AND B.PARENT_ID = 0 ");
        sqlStat.append("AND A.ROOT_TYPE = ? ");
        //sqlStat.append(this.getIDFilterWhereClause("A"));
        if(!Utility.isEmpty(orderBy)){
          sqlStat.append(" ORDER BY "+orderBy);
        }
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, 2, DmsRoot.PUBLIC_ROOT);
        rs = preStat.executeQuery();
        while (rs.next()) {
          DmsRoot tmpDmsRoot = new DmsRoot();
          tmpDmsRoot.setID(getResultSetInteger(rs, "ID"));
          tmpDmsRoot.setRootName(getResultSetString(rs, "ROOT_NAME"));
          tmpDmsRoot.setRootType(getResultSetString(rs, "ROOT_TYPE"));
          tmpDmsRoot.setRootDesc(getResultSetString(rs, "DESCRIPTION"));
          tmpDmsRoot.setRootFolderID(getResultSetInteger(rs, "ROOT_FOLDER_ID"));
          tmpDmsRoot.setLocID(getResultSetInteger(rs, "LOC_ID"));
          tmpDmsRoot.setStorageLimit(getResultSetDouble(rs, "STORAGE_LIMIT"));
          tmpDmsRoot.setOwnerID(getResultSetInteger(rs, "OWNER_ID"));
          tmpDmsRoot.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpDmsRoot.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpDmsRoot.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpDmsRoot.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpDmsRoot.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpDmsRoot.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpDmsRoot.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsRoot.getCreatorID()));
          tmpDmsRoot.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsRoot.getUpdaterID()));
          result.add(tmpDmsRoot);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  /**
   * Gets list of personal dmsRoot objects
   * 
   * @return list of personal dmsRoot objects
   * @throws ApplicationException
   */
  public synchronized List getAllPersonalRootList() throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.ROOT_NAME, A.ROOT_TYPE, A.LOC_ID, A.STORAGE_LIMIT, A.OWNER_ID, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE, B.DESCRIPTION, B.ID AS ROOT_FOLDER_ID ");
        sqlStat.append("FROM   DMS_ROOT A, DMS_DOCUMENT B ");
        sqlStat.append("WHERE  A.ID = B.ROOT_ID AND B.PARENT_ID = 0 ");
        sqlStat.append("       AND A.ROOT_TYPE = ? ");
        //sqlStat.append(this.getIDFilterWhereClause("A"));
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        //this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, 1, DmsRoot.PERSONAL_ROOT);
        rs = preStat.executeQuery();
        while (rs.next()) {
          DmsRoot tmpDmsRoot = new DmsRoot();
          tmpDmsRoot.setID(getResultSetInteger(rs, "ID"));
          tmpDmsRoot.setRootName(getResultSetString(rs, "ROOT_NAME"));
          tmpDmsRoot.setRootType(getResultSetString(rs, "ROOT_TYPE"));
          tmpDmsRoot.setRootDesc(getResultSetString(rs, "DESCRIPTION"));
          tmpDmsRoot.setRootFolderID(getResultSetInteger(rs, "ROOT_FOLDER_ID"));
          tmpDmsRoot.setLocID(getResultSetInteger(rs, "LOC_ID"));
          tmpDmsRoot.setStorageLimit(getResultSetDouble(rs, "STORAGE_LIMIT"));
          tmpDmsRoot.setOwnerID(getResultSetInteger(rs, "OWNER_ID"));
          tmpDmsRoot.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpDmsRoot.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpDmsRoot.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpDmsRoot.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpDmsRoot.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpDmsRoot.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpDmsRoot.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsRoot.getCreatorID()));
          tmpDmsRoot.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsRoot.getUpdaterID()));
          result.add(tmpDmsRoot);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }


 /**
  * Gets the abstract object of dmsRoot by ID and no matter what record status.
  * 
  * @param id the dmsRoot ID
  * @return abstract object of dmsRoot matched.
  * @throws ApplicationException
  */
  public synchronized AbstractBaseObject getObjectByIDIgnoreRecordStatus(Integer id) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.ROOT_NAME, A.ROOT_TYPE, A.LOC_ID, A.STORAGE_LIMIT, A.OWNER_ID, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE, B.DESCRIPTION, B.ID AS ROOT_FOLDER_ID ");
        sqlStat.append("FROM   DMS_ROOT A, DMS_DOCUMENT B ");
        sqlStat.append("WHERE  A.ID = ? AND A.ID = B.ROOT_ID AND B.PARENT_ID = 0 ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, id);
        rs = preStat.executeQuery();
        if (rs.next()) {
          DmsRoot tmpDmsRoot = new DmsRoot();
          tmpDmsRoot.setID(getResultSetInteger(rs, "ID"));
          tmpDmsRoot.setRootName(getResultSetString(rs, "ROOT_NAME"));
          tmpDmsRoot.setRootType(getResultSetString(rs, "ROOT_TYPE"));
          tmpDmsRoot.setRootDesc(getResultSetString(rs, "DESCRIPTION"));
          tmpDmsRoot.setRootFolderID(getResultSetInteger(rs, "ROOT_FOLDER_ID"));
          tmpDmsRoot.setLocID(getResultSetInteger(rs, "LOC_ID"));
          tmpDmsRoot.setStorageLimit(getResultSetDouble(rs, "STORAGE_LIMIT"));
          tmpDmsRoot.setOwnerID(getResultSetInteger(rs, "OWNER_ID"));
          tmpDmsRoot.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpDmsRoot.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpDmsRoot.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpDmsRoot.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpDmsRoot.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpDmsRoot.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpDmsRoot.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsRoot.getCreatorID()));
          tmpDmsRoot.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsRoot.getUpdaterID()));
          return(tmpDmsRoot);
        } else {
          return null;
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  
  /**
   * Gets the abstract object of DmsRoot which is public and record status is not inactive by public folder name.
   * 
   * @param publicFolderName the public folder name
   * @return 
   * @throws ApplicationException
   */
  public synchronized AbstractBaseObject getObjectByName(String publicFolderName) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.ROOT_NAME, A.ROOT_TYPE, A.LOC_ID, A.STORAGE_LIMIT, A.OWNER_ID, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE, B.DESCRIPTION, B.ID AS ROOT_FOLDER_ID ");
        sqlStat.append("FROM   DMS_ROOT A, DMS_DOCUMENT B ");
        sqlStat.append("WHERE  A.ROOT_NAME = ? AND (A.RECORD_STATUS = ? OR A.RECORD_STATUS = ?) AND A.ID = B.ROOT_ID AND B.PARENT_ID = 0 AND ROOT_TYPE = 'U' ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, publicFolderName);
        this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, 3, GlobalConstant.RECORD_STATUS_PENDING);
        rs = preStat.executeQuery();
        if (rs.next()) {
          DmsRoot tmpDmsRoot = new DmsRoot();
          tmpDmsRoot.setID(getResultSetInteger(rs, "ID"));
          tmpDmsRoot.setRootName(getResultSetString(rs, "ROOT_NAME"));
          tmpDmsRoot.setRootType(getResultSetString(rs, "ROOT_TYPE"));
          tmpDmsRoot.setRootDesc(getResultSetString(rs, "DESCRIPTION"));
          tmpDmsRoot.setRootFolderID(getResultSetInteger(rs, "ROOT_FOLDER_ID"));
          tmpDmsRoot.setLocID(getResultSetInteger(rs, "LOC_ID"));
          tmpDmsRoot.setStorageLimit(getResultSetDouble(rs, "STORAGE_LIMIT"));
          tmpDmsRoot.setOwnerID(getResultSetInteger(rs, "OWNER_ID"));
          tmpDmsRoot.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpDmsRoot.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpDmsRoot.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpDmsRoot.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpDmsRoot.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpDmsRoot.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpDmsRoot.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsRoot.getCreatorID()));
          tmpDmsRoot.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsRoot.getUpdaterID()));
          return(tmpDmsRoot);
        } else {
          //throw new ApplicationException(ErrorConstant.DB_RECORD_NOT_FOUND_ERROR);
          return null;
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  
  /**
   * Gets the abstract object of dmsRoot by version ID.
   * 
   * @param versionID the version ID 
   * @return the abstarct object of dmsRoot matched.
   * @throws ApplicationException
   */
  public synchronized AbstractBaseObject getRootByVersionID(Integer versionID) throws ApplicationException {
     PreparedStatement preStat = null;
     ResultSet rs = null;
     StringBuffer sqlStat = new StringBuffer();

     synchronized(dbConn) {
       try {
         sqlStat.append("SELECT A.ID, A.ROOT_NAME, A.ROOT_TYPE, A.LOC_ID, A.STORAGE_LIMIT, A.OWNER_ID, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE, B.DESCRIPTION, B.ID AS ROOT_FOLDER_ID ");
         sqlStat.append("FROM   DMS_ROOT A, DMS_DOCUMENT B , DMS_VERSION V ");
         sqlStat.append("WHERE V.DOCUMENT_ID = B.ID ");
         sqlStat.append("AND B.ROOT_ID = A.ID ");
        // sqlStat.append("AND (A.RECORD_STATUS = ? OR A.RECORD_STATUS = ? ) ");
         sqlStat.append("AND V.ID = ? ");
         preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
         
        // this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        // this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_PENDING);
         this.setPrepareStatement(preStat, 1, versionID);
         rs = preStat.executeQuery();
         if (rs.next()) {
           DmsRoot tmpDmsRoot = new DmsRoot();
           tmpDmsRoot.setID(getResultSetInteger(rs, "ID"));
           tmpDmsRoot.setRootName(getResultSetString(rs, "ROOT_NAME"));
           tmpDmsRoot.setRootType(getResultSetString(rs, "ROOT_TYPE"));
           tmpDmsRoot.setRootDesc(getResultSetString(rs, "DESCRIPTION"));
           tmpDmsRoot.setRootFolderID(getResultSetInteger(rs, "ROOT_FOLDER_ID"));
           tmpDmsRoot.setLocID(getResultSetInteger(rs, "LOC_ID"));
           tmpDmsRoot.setStorageLimit(getResultSetDouble(rs, "STORAGE_LIMIT"));
           tmpDmsRoot.setOwnerID(getResultSetInteger(rs, "OWNER_ID"));
           tmpDmsRoot.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
           tmpDmsRoot.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
           tmpDmsRoot.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
           tmpDmsRoot.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
           tmpDmsRoot.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
           tmpDmsRoot.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
           tmpDmsRoot.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsRoot.getCreatorID()));
           tmpDmsRoot.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsRoot.getUpdaterID()));
           return(tmpDmsRoot);
         } else {
           //throw new ApplicationException(ErrorConstant.DB_RECORD_NOT_FOUND_ERROR);
           return null;
         }
       } catch (ApplicationException appEx) {
         throw appEx;
       } catch (SQLException sqle) {
         log.error(sqle, sqle);
         throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
       } catch (Exception e) {
         log.error(e, e);
         throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
       } finally {
         try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
         try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
       }
     }
   }
  
  public synchronized AbstractBaseObject getByFolderName(String folderName) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.ROOT_NAME, A.ROOT_TYPE, A.LOC_ID, A.STORAGE_LIMIT, A.OWNER_ID, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE, B.DESCRIPTION, B.ID AS ROOT_FOLDER_ID ");
        sqlStat.append("FROM   DMS_ROOT A, DMS_DOCUMENT B ");
        sqlStat.append("WHERE  A.ROOT_NAME = ? AND (A.RECORD_STATUS = ? OR A.RECORD_STATUS = ?) AND A.ROOT_TYPE = ? AND A.ID = B.ROOT_ID AND B.PARENT_ID = 0 ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, folderName);
        this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, 3, GlobalConstant.RECORD_STATUS_PENDING);
        this.setPrepareStatement(preStat, 4, DmsRoot.PUBLIC_ROOT);
        rs = preStat.executeQuery();
        if (rs.next()) {
          DmsRoot tmpDmsRoot = new DmsRoot();
          tmpDmsRoot.setID(getResultSetInteger(rs, "ID"));
          tmpDmsRoot.setRootName(getResultSetString(rs, "ROOT_NAME"));
          tmpDmsRoot.setRootType(getResultSetString(rs, "ROOT_TYPE"));
          tmpDmsRoot.setRootDesc(getResultSetString(rs, "DESCRIPTION"));
          tmpDmsRoot.setRootFolderID(getResultSetInteger(rs, "ROOT_FOLDER_ID"));
          tmpDmsRoot.setLocID(getResultSetInteger(rs, "LOC_ID"));
          tmpDmsRoot.setStorageLimit(getResultSetDouble(rs, "STORAGE_LIMIT"));
          tmpDmsRoot.setOwnerID(getResultSetInteger(rs, "OWNER_ID"));
          tmpDmsRoot.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpDmsRoot.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpDmsRoot.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpDmsRoot.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpDmsRoot.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpDmsRoot.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpDmsRoot.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsRoot.getCreatorID()));
          tmpDmsRoot.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsRoot.getUpdaterID()));
          return(tmpDmsRoot);
        } else {
          //throw new ApplicationException(ErrorConstant.DB_RECORD_NOT_FOUND_ERROR);
          return null;
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }


  /**
   * Checks the record is exist or not by root ID provided
   * 
   * @param docID the dmsRoot ID
   * @return true if exists, false if not
   * @throws ApplicationException
   */
   public synchronized boolean checkDocExist(Integer docID) throws ApplicationException {
       PreparedStatement preStat = null;
       ResultSet rs = null;
       StringBuffer sqlStat = new StringBuffer();
       boolean docExist = false;

       synchronized (dbConn) {
         try {
           sqlStat.append("SELECT A.ID ");
           sqlStat.append("FROM   DMS_ROOT A ");
           sqlStat.append("WHERE  A.ID = ? ");
           preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
           this.setPrepareStatement(preStat, 1, docID);


           //format the sql for any 'LIKE' statement contained
           sqlStat = this.getFormattedSQL(sqlStat.toString());
           rs = preStat.executeQuery();

           if (rs.next()) {
             docExist = true;
           } else {
             docExist = false;
           }
           return docExist;

         }catch (SQLException sqle) {
           log.error(sqle, sqle);
           throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle,
                                          sqle.toString());
         } catch (Exception e) {
           log.error(e, e);
           throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
         } finally {
           try {
             rs.close();
           } catch (Exception ignore) {} finally {
             rs = null;
           }
           try {
             preStat.close();
           } catch (Exception ignore) {} finally {
             preStat = null;
           }
         }
       }
     }
   
  /**
   * Delete the table record 'DMS_ROOT' by root ID
   * 
   * @param rootID the root ID
   * @throws ApplicationException
   */
  public synchronized void hardDelete(Integer rootID) throws ApplicationException {

    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();

    synchronized (dbConn) {
      try {
        int updateCnt = 0;
        sqlStat.append("DELETE ");
        sqlStat.append("FROM   DMS_ROOT ");
        sqlStat.append("WHERE  ID=?  ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, rootID);

        updateCnt = preStat.executeUpdate();

      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_DELETE_ERROR, e);
      } finally {
        try {
          preStat.close();
        } catch (Exception ignore) {} finally {
          preStat = null;
        }
      }
    }
  }
  
  public Integer getPersonalRootIDByCurUserID() throws ApplicationException{
		 PreparedStatement preStat = null;
		 StringBuffer sqlStat = new StringBuffer();
		 ResultSet rs = null;
		 Integer rootID = new Integer(-1);
		 Integer CurUserID = sessionContainer.getUserRecordID();
		 synchronized (dbConn) {
			 try {
			        sqlStat.append("SELECT A.ID ");
			        sqlStat.append("FROM   DMS_ROOT A ");
			        sqlStat.append("WHERE  A.OWNER_ID = ? AND A.ROOT_TYPE = ?");
//			        format the sql for any 'LIKE' statement contained
			        sqlStat = this.getFormattedSQL(sqlStat.toString());

			        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
			        int i=0;
			        this.setPrepareStatement(preStat, ++i, CurUserID);
			        this.setPrepareStatement(preStat, ++i, DmsRoot.PERSONAL_ROOT);
			        rs = preStat.executeQuery();
			        while (rs.next()){ 
			            rootID = getResultSetInteger(rs, "ID");
			        }
			 }catch (ApplicationException appEx) {
			        throw appEx;
		      } catch (SQLException sqle) {
		        log.error(sqle, sqle);
		        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
		      } catch (Exception e) {
		        log.error(e, e);
		        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
		      }
		 }
		 return rootID;
	 }


}

